import axios from 'axios'
import store from '../store'
import JSONBig from 'json-bigint'

const request = axios.create({
    // 接口的基准路径
    // baseURL: 'http://ttapi.research.itcast.cn',
    baseURL: 'http://localhost:8080/',

    // 自定义后端返回的原始数据
    // data:后端返回的园市数据，说白了就是JSON格式的字符串
    transformResponse: [function(data) {
        try {
            return JSONBig.parse(data)
        } catch (err) {
            return data
        }
    }]
})

request.interceptors.request.use((config) => {
    const { user } = store.state
    if (user && user.token) {
        config.headers.Authorization = `Bearer ${user.token}`
    }

    return config
}, error => {
    return Promise.reject(error)
})

export default request